//
//  NetworkTool.swift
//  woi
//
//  Created by 中创 on 2020/4/20.
//  Copyright © 2020 中创-慧谷. All rights reserved.
//

import UIKit

import Alamofire

typealias FSResponseSuccess = (_ response: Any) -> Void
typealias FSResponseFail = (_ error: Any) -> Void
typealias FSNetworkStatus = (_ NetworkStatus: Int32) -> Void
typealias FSProgressBlock = (_ progress: Int32) -> Void

@objc enum NetworkStatus: Int32 {
    case unknown          = -1//未知网络
    case notReachable     = 0//网络无连接
    case wwan             = 1//2，3，4G网络
    case wifi             = 2//wifi网络
}
enum HTTPMethodType{
    case GET
    case POST
}

class NetworkTool {

    class func requestData(url:String, params: [String: Any], method:HTTPMethodType, success: @escaping FSResponseSuccess,error: @escaping FSResponseFail) {
        
        var httpMethod = HTTPMethod.get
        if method == .POST {
            httpMethod = HTTPMethod.post
        }
        let header = HTTPHeaders()
        AF.request(url, method: httpMethod, parameters: params, encoding: URLEncoding.default, headers: header, interceptor: nil, requestModifier: nil).responseJSON { (response) in
            
            var urlString = url
            urlString.append("?")
            for (key, value) in params{
                urlString.append("\(key)=\(value)&")
            }
            print("接口：\(urlString)")
            
            
            switch response.result {
            case .success:
                success(response.value as Any)
            case .failure:
                let statusCode = response.response?.statusCode
                print("出错了....")
                print(statusCode as Any)
                error(response.value as Any)
            }
        }

    }


    
    
}
